//app.js
var qcloud = require('./vendor/wafer2-client-sdk/index')
var config = require('./config')

App({
    onLaunch: function () {
        // qcloud.setLoginUrl(config.service.loginUrl)
        wx.login({
          success:function(res){
            // console.log(res.code);
            if (res.code) {
              //发起网络请求
              wx.request({
                url: 'https://meile1990.meileholdings.com/miku/index.php/wxlogin',
                data: {
                  code: res.code
                },
                success :function(res){
                  //console.log(res.data);
                  var openid = res.data['openid'];
                  wx.setStorage({
                    key: 'openid',
                    data: openid,
                  })
                  
                  //判断授权
                  wx.getSetting({
                    success(res) {
                      if (!res.authSetting['scope.userInfo']) {
                        //console.log("不同意")
                        wx.request({
                          url: 'https://meile1990.meileholdings.com/miku/index.php/adduser',
                          data: {
                            openid: openid,
                            nick_name: "用户未同意"
                          },
                          method: 'POST',
                          success: function (res) {
                            // console.log(res.data);
                            wx.setStorage({
                              key: 'userid',
                              data: res.data,
                            })
                          }
                        })

                      }else{

                        wx.getUserInfo({
                          success: function (res) {
                            // console.log(res)
                            wx.request({
                              url: 'https://meile1990.meileholdings.com/miku/index.php/adduser',
                              data: {
                                openid: openid,
                                nick_name: res.userInfo.nickName
                              },
                              method: 'POST',
                              success: function (res) {
                                // console.log(res.data);
                                wx.setStorage({
                                  key: 'userid',
                                  data: res.data,
                                })
                              }
                            })
                          }
                        })
                        
                      }
                    }
                  })


                
                }
              })
            } else {
              console.log('获取用户登录态失败！' + res.errMsg)
            }
          }
        })
    },
    data:{
      service: 'https://meile1990.meileholdings.com/miku',
      hide:false,
      orderId:0
    }
    
})